/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef SYSTEM_THREAD_WIN32_H_
#define SYSTEM_THREAD_WIN32_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <process.h>


namespace Ucop {
namespace System {

    class Runnable;

    class ThreadImpl
    {
    public:
        /* Thread priority */
        enum Priority
        {
            PRIO_LOWEST_IMPL = THREAD_PRIORITY_LOWEST,
            PRIO_LOW_IMPL = THREAD_PRIORITY_BELOW_NORMAL,
            PRIO_NORMAL_IMPL = THREAD_PRIORITY_NORMAL,
            PRIO_HIGH_IMPL = THREAD_PRIORITY_ABOVE_NORMAL,
            PRIO_HIGHEST_IMPL = THREAD_PRIORITY_HIGHEST
        };
        /* Thread ID */
        typedef DWORD   ID;

        ThreadImpl(Runnable *_Runnable);
        ~ThreadImpl(void);


        bool StartImpl(void);
        void JoinImpl(void);
        bool JoinableImpl(void);
        bool SetPriorityImpl(int _Priority);
        ID GetIDImpl(void) const { return _Myid; }

        static ID GetCurrentIDImpl(void);
        static void SleepImpl(long _Miliseconds);


    private:
        static unsigned int __stdcall _Proc(void* arg);

        ID _Myid;
        uintptr_t _Myhandle;
        Runnable *_Myrunnable;
    };

}}

#endif // SYSTEM_THREAD_WIN32_H_
